<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2023-02-07 09:34:24
 * @Last Modified time: 2023-02-23 15:04:05
 */
namespace app\sys\controller\store;

use app\sys\controller\Base;
use think\facade\{
    App
};
use app\common\services\store\OrderService;
use app\common\services\store\OrderAddressService;
use app\common\services\store\OrderDeliveryService;
/**
 * 商城-订单
 * Class Order 
 * @package app\sys\controller\store
 */
class Order extends Base
{
    /**
     * Order constructor.
     * @param App $app
     */
    public function __construct(App $app,OrderService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 商品订单列表
     * @method(GET)
     */
    public function list()
    {
        $param = $this->request->getMore([
            ['search_key',''],
            ['search_value',''],
            ['soucre',''],
            ['order_status',''],
            ['pay_type',''],
            ['create_time',[]],
        ]);
        $this->success($this->service->getOrderList($param));
    }

    /**
     * 订单详细 
     * @method(GET)
     */
    public function detail(){
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        $this->success($this->service->getOrderDetail($id));
    }

    /**
     * 关闭订单
     * @method(PUT)
     */
    public function closeOrder(){
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        if ($this->service->closeOrder($id)) {
            $this->success('关闭成功');
        }
        $this->error($this->service->getError() ?: '关闭失败');
    }

    /**
     * 获取订单发货单
     * @noAuth(true)
     * @method(GET)
     */
    public function getOrderDeliveryDetail(){
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        $order = $this->service->getOrderDetail($id);
        if ($order['delivery_status']['value']) {
            $this->error('该订单已发货完成!');
        }
        if ($order['delivery_type']['value'] == 'express') {
            $detail = OrderAddressService::instance()->getAddressDetail([
                'order_id'=>$id
            ]);
        }else{
            $detail = $order['extract'];
        }
        $delivery = OrderDeliveryService::instance()->getDeliverySelect([
            'order_id'=>$id
        ]);
        $this->success(compact('detail','delivery'));
    }

    /**
     * 调整订单价格
     * @method(PUT)
     */
    public function updatePrice(){
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        $data = $this->request->postMore([
            'order_pay_price',
            'express_free',
            'express_price',
        ]);
        if ($this->service->updatePrice($id,$data)) {
            $this->success('调整成功');
        }
        $this->error($this->service->getError() ?: '调整失败');
    }

    /**
     * 线下支付
     * @method(POST)
     */
    public function offlinePayment(){
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        $data = $this->request->postMore([
            'pay_price'
        ]);
        if ($this->service->offlinePayment($id,$data)) {
            $this->success('支付成功');
        }
        $this->error($this->service->getError() ?: '支付失败');
    }

    /**
     * 修改收货地址
     * @method(PUT)
     */
    public function updateAddress(){
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        $data = $this->request->postMore([
            'name',
            'phone',
            'detail',
            'region',
        ]);
        $service = OrderAddressService::instance();
        $data = $service->getAddressRegionData($data);
        if ($service->update(['order_id'=>$id],$data)) {
            $this->success('修改成功');
        }
        $this->error($service->getError() ?: '修改失败');
    }

    /**
     * 发货
     * @method(PUT)
     */
    public function orderDelivery(){
        if (!$orderId = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        $data = $this->request->postMore([
            'order_goods_id',
            'delivery_no',
            'delivery_mode',
            'express_company_id',
        ]);
        $service = OrderDeliveryService::instance();
        if ($service->orderGoodsDelivery($orderId,$data)) {
            $this->success('发货成功');
        }
        $this->error($service->getError() ?: '发货失败');
    }

    /**
     * 取货核销
     * @method(PUT)
     */
    public function orderExtract(){
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        $data = $this->request->postMore([
            'extract_code',
            'order_goods_id'
        ]);
        $service = OrderDeliveryService::instance();
        if ($service->orderGoodsDelivery($id,$data)) {
            $this->success('核销成功');
        }
        $this->error($service->getError() ?: '核销失败');
    }

    /**
     * 审核：用户取消订单
     * @method(PUT)
     */
    public function cancelOrder()
    {
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        $data = $this->request->postMore([
            'is_cancel'
        ]);
        if ($this->service->cancelOrder([
            'order_id'=>$id,
            'is_cancel'=>$data['is_cancel']
        ])) {
            $this->success('操作成功');
        }
        $this->error($service->getError() ?: '操作失败');
    }

    /**
     * 备注
     * @method(PUT)
     */
    public function updateRemark()
    {
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        $data = $this->request->postMore([
            'remark'
        ]);
        if ($this->service->update($id,[
            'remark'=>$data['remark']
        ])) {
            $this->success('操作成功');
        }
        $this->error($service->getError() ?: '操作失败');
    }
    
    /**
     * 确定收货
     * @method(PUT)
     */
    public function receiptOrder(){
        $id = $this->request->param('id');
        if ($this->service->receiptOrder($id)) {
            $this->success('处理成功');
        }
        $this->error($this->service->getError() ?: '处理失败');
    }

    /**
     * 订单收货地址详细 
     * @method(GET)
     */
    public function getOrderAddressDetail(){
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        $this->success(OrderAddressService::instance()->getAddressDetail([
            'order_id'=>$id
        ]));
    }

}
